package com.ifengchao.dao.impl;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ifengchao.dao.IBatisBaseDao1;
import com.ifengchao.dao.IBatisBaseDao2;
import com.ifengchao.dao.IBatisBaseDao3;
import com.ifengchao.dao.iface.DealCreativesDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by CSJ11 on 2017/8/29.
 */
@Repository
public class DealCreativesDaoImpl implements DealCreativesDao {
    private static final Logger logger = Logger.getLogger(DealCreativesDaoImpl.class);

    @Autowired(required = true)
    private IBatisBaseDao1 iBatisBaseDao1;

//    @Autowired(required = true)
//    private IBatisBaseDao2 iBatisBaseDao2;

    @Autowired(required=true)
    private IBatisBaseDao3 iBatisBaseDao3;

    @Override
    public List<String> queryAllPlanId() {
        logger.info("查询所有计划ID");
        List<String> planIdList = iBatisBaseDao1.getSqlMapClientTemplate().queryForList("dealCreativesSql.queryAllPlanId");
        return planIdList;
    }



    @Override
    public void UpdateCreatives(List<Map<String, Object>> propMaps) {
        logger.info("更新创意");
        final List<Map<String, Object>> tempList = propMaps;
        try {
            if (tempList != null) {
                iBatisBaseDao1.getSqlMapClientTemplate().execute(
                        new SqlMapClientCallback() {
                            public Object doInSqlMapClient(SqlMapExecutor executor) throws
                                    SQLException {
                                executor.startBatch();
                                for (int i = 0, n = tempList.size(); i < n; i++) {
                                    executor.update("dealCreativesSql.UpdateCreatives", tempList.get(i));
                                    executor.update("dealCreativesSql.insertCreativesDay", tempList.get(i));
                                }
                                executor.executeBatch();
                                return null;
                            }
                        }
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String queryAdUserId(String planId) {
        return iBatisBaseDao1.getSqlMapClientTemplate().queryForObject("dealCreativesSql.queryAdUserId",planId).toString();
    }

    @Override
    public Double queryServiceRate(String adId) {
        return Double.valueOf(iBatisBaseDao3.getSqlMapClientTemplate().queryForObject("dealCreativesSql.queryServiceRate",adId).toString());
    }
}
